package com.xff.Controller;


import com.xff.bean.AdminBean;
import com.xff.bean.ApplyBean;
import com.xff.bean.BookBean;
import com.xff.bean.SubscribeBean;
import com.xff.dao.AdminDao;
import com.xff.dao.ApplyDao;
import com.xff.dao.BookDao;
import com.xff.dao.SubscribeDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
public class SubscribeController {

    @PostMapping("/SelectSubscribeServlet")
    public  String SelectSubscribeServlet(HttpServletRequest request, HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            int tip = Integer.parseInt(request.getParameter("tip"));
            String name = request.getParameter("name");
            ArrayList<SubscribeBean> subscribeBeans= new SubscribeDao().get_SubscribeListInfo(tip,name);
            //将获取的结果存入请求中
            request.setAttribute("data", subscribeBeans);
            return "admin_subscribe";
        }else{
            return "redirect:/login";
        }
    }


    @GetMapping("/Subscribe_Asce")
    public String Subscribe_Asce(HttpServletRequest request, HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            ArrayList<SubscribeBean> subscribeBeans=new SubscribeDao().get_SubscribeRank(1);
            //将获取的结果存入请求中
            request.setAttribute("rank", subscribeBeans);
            return "admin_subscribe";
        }else{
            return "redirect:/login";
        }
    }

    @GetMapping("/Subscribe_Desc")
    public String Subscribe_Desc(HttpServletRequest request, HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            ArrayList<SubscribeBean> subscribeBeans=new SubscribeDao().get_SubscribeRank(0);
            //将获取的结果存入请求中
            request.setAttribute("rank", subscribeBeans);
            return "admin_subscribe";
        }else{
            return "redirect:/login";
        }
    }

    /*
    * 预约
    * */
    @ResponseBody
    @GetMapping("/requestsubscribe")
    public String requestsubscribe(HttpServletRequest request){

        String username=request.getParameter("username");
        int bid=Integer.parseInt(request.getParameter("bid"));

        /*SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String date=format.format(new Date());*/

        if(new SubscribeDao().get_SubscribeListInfo(username,bid).size()>0){
            return "hasSubscribe";
        }
        else {
            AdminDao adminDao=new AdminDao();
            AdminBean adminBean=adminDao.get_AidInfo3(username);
            BookDao bookdao = new BookDao();
            BookBean bookBean=bookdao.get_BookInfo(bid);

            SubscribeBean subscribeBean=new SubscribeBean(adminBean.getAid(),bookBean.getBid(),bookBean.getCard(),bookBean.getName(),adminBean.getUsername(),adminBean.getName());
            SubscribeDao subscribeDao=new SubscribeDao();
            subscribeDao.addSubBook(subscribeBean);
            return "ok";
        }
    }

    @ResponseBody
    @GetMapping("/getSubscribeList")
    public  ArrayList<SubscribeBean> getSubscribeList(HttpServletRequest request, HttpSession session){
        String username = request.getParameter("username");
        ArrayList<SubscribeBean> subscribeBeans=new SubscribeDao().get_SubscribeListInfo(username);

        return subscribeBeans;
    }

    @ResponseBody
    @GetMapping("/deleteSubscribe")
    public String deleteSubscribe(HttpServletRequest request,HttpSession session){
        int subscribeid =Integer.parseInt(request.getParameter("subscribeid"));
        new SubscribeDao().deleteSubscribe(subscribeid);
        return "ok";
    }


}
